<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()" :disabled="catId<=3" >新增</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="attrName"
        header-align="center"
        align="center"
        label="属性名" >
      </el-table-column>
      <el-table-column
        prop="searchType"
        header-align="center"
        align="center"
        label="可检索" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.searchType=== 1"  type="success"><i class="el-icon-success"></i></el-tag>
          <el-tag v-else type="info"><i class="el-icon-circle-close"></i></el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="showDesc"
        header-align="center"
        align="center"
        label="快速展示" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.showDesc=== 1"  type="success"><i class="el-icon-success"></i></el-tag>
          <el-tag v-else type="info"><i class="el-icon-circle-close"></i></el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="valueSelect"
        header-align="center"
        align="center"
        label="可选值列表" width="250">
        <template slot-scope="scope">
          <!-- 循环渲染tag标签 -->
          <el-tag size="medium" v-for="(item ,index ) in scope.row.valueSelect" :key="index" closable @close="handleClose(scope.row,index)">{{item}}</el-tag>
          <!-- 输入的文本框 -->
          <el-input
            class="input-new-tag"
            v-if="scope.row.inputVisible"
            v-model="scope.row.inputValue"
            ref="saveTagInput"
            size="mini"
            @keyup.enter.native="handleInputConfirm(scope.row)"
            @blur="handleInputConfirm(scope.row)"
          >
          </el-input>
          <!-- 添加的按钮 -->
          <el-button v-else class="button-new-tag" size="mini" @click="showInput(scope.row)">+ New Tag</el-button>
        </template>
      </el-table-column>
        
      <el-table-column
        prop="valueType"
        header-align="center"
        align="center"
        label="可多选"  width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.valueType === 1 "  type="success"><i class="el-icon-success"></i></el-tag>
          <el-tag v-else type="info"><i class="el-icon-circle-close"></i></el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="catelogId"
        header-align="center"
        align="center"
        label="所属分类">
      </el-table-column>
      <el-table-column
        prop="attrGroupId"
        header-align="center"
        align="center"
        label="属性分组">
      </el-table-column> -->
      <!-- <el-table-column
        prop="attrGroupSort"
        header-align="center"
        align="center"
        label="属性组内排序">
      </el-table-column> -->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.attrId,scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.attrId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" :catId="catId" @updatedata="getDataList" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './components/AttrAddUpdate'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [
        ],
        pageIndex: 1,
        catId:'0',
        pageSize: 10,
        totalPage: 0,
        dataListSelections: [],
        addOrUpdateVisible: false,
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      async  getDataList(id) {
          this.catId  = id ? id : 1
          // console.log("id",id)
        if(this.$route.path === '/baseattr'){
        const {data:res} = await this.$http.get(`/dqxmall/prosysattr/base/list/${this.catId}?page=${this.pageIndex}&limit=${this.pageSize}&key=${this.dataForm.key}`)
        console.log("base",res)
        res.page.list.forEach(item => {
            item.valueSelect = item.valueSelect ? item.valueSelect.split('，') : [] 
            // 控制文本框的显示与隐藏
            item.inputVisible = false
            item.inputValue = ''
          });
        this.dataList = res.page.list
        this.totalPage = res.page.totalCount
        }
        if(this.$route.path === '/salesattr'){
          const {data:res} = await this.$http.get(`/dqxmall/prosysattr/sale/list/${this.catId}?page=${this.pageIndex}&limit=${this.pageSize}&key=${this.dataForm.key}`)
          console.log("sale",res)
          res.page.list.forEach(item => {
            item.valueSelect = item.valueSelect ? item.valueSelect.split('，') : [] 
            // 控制文本框的显示与隐藏
            item.inputVisible = false
            item.inputValue = ''
          });
        this.dataList = res.page.list
        this.totalPage = res.page.totalCount
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id,data) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,data)
        })
      },
      // tag文本框失去焦点触发
      async  handleInputConfirm(row){
        if(row.inputValue.trim().length === 0){
          row.inputValue =  ''
        row.inputVisible = false
        return 
        }
        // 不是空进行后续处理
        row.valueSelect.push(row.inputValue.trim())
        row.inputValue = ''
        row.inputVisible = false
        // 发起请求保存这次操作
         this.saveAttr(row)
      },
      // 将多可选值保存操数据库中
     async   saveAttr(row){
       const {data:res} = await this.$http.post("/dqxmall/prosysattr/update",{
          attrId: row.attrId,
          attrType : row.attrType,
          valueSelect : row.valueSelect.join("，")
         })
          if(res.code !== 0) return this.$message.error("修改参数项失败")
          this.$message.success("修改参数项成功")
      },
      // 删除参数项
      handleClose(row,index){
        row.valueSelect.splice(index,1)
        this.saveAttr(row)
      },
      // 点击增加按钮显示输入框
      showInput(row){
        row.inputVisible = true
        // 让文本框获得焦点
        // .$nextTick方法的作用是当页面数据渲染完成之后，才会指定回调用中的代码
         this.$nextTick(() => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.attrId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async() => {
          const {data:res} = await this.$http.delete("/dqxmall/prosysattr/delete",{data:ids})
          if(res.msg === 'success'){
            this.getDataList()
            this.$message.success("删除成功")
          }
          
        })
      }
    }
  }
</script>
<style lang="less" scope>
.el-tag{
  margin: 3px;
}
.input-new-tag{
  width:100px;
}
</style>
